/*
 * Copyright (c) 2001, Aslak Hellesøy, BEKK Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of BEKK Consulting nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package middlegen;

import java.util.HashMap;
import java.util.Map;

/**
 * Baseclass for Relationship role decorators. Subclasses can add additional functionality which extends the information that can
 * be retrieved from the database.
 *
 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
 * @version $Revision: 1.1 $
 */
public class RelationshipRoleDecorator extends PreferenceAware implements RelationshipRole {

   /** The relationship to decorate. */
   private final RelationshipRole _subject;
   
   private Table _target;
   private Table _origin;
   
   private Map _originTables = new HashMap();
   
   private Map _targetTables = new HashMap();
   

   /**
    * Initialize the decorator with the given column.
    *
    * @param subject The column object to decorate.
    */
   public RelationshipRoleDecorator(RelationshipRole subject) {
      if (!(subject instanceof DbRelationshipRole)) {
         throw new IllegalArgumentException("subject must be of class " + DbRelationshipRole.class.getName() + ". Was:" + subject.getClass().getName());
      }
      _subject = subject;
   }
   
   /**
    * Add an association from a plugin to an origin table.
    * 
    * @param plugin The plugin.
    * @param origin The origin table.
    */
   public void addOriginTable( Plugin plugin, Table origin ) {
	   _origin = origin;
	   _originTables.put( plugin, origin );
   }
   
   /**
    * Add an association from a plugin to an target table.
    * 
    * @param plugin The plugin.
    * @param target The target table.
    */
   public void addTargetTable( Plugin plugin, Table target ) {
	   _target = target;
	   _targetTables.put( plugin, target );
   }
   
   /**
    * @see RelationshipRole#isFkPk()
    */
   public boolean isFkPk() {
	   return _subject.isFkPk();
   }

   /**
    * @see RelationshipRole#getSuffix()
    */
   public String getSuffix() {
	   return _subject.getSuffix();
   }

   /**
    * @see RelationshipRole#getName(Plugin)
    */
   public String getName(Plugin plugin) {
	   return _subject.getName( plugin );
   }

   /**
    * @see RelationshipRole#getName()
    */
   public String getName() {
	   return _subject.getName();
   }

   /**
    * @see RelationshipRole#getTargetRole()
    */
   public RelationshipRole getTargetRole() {
	   return _subject.getTargetRole();
   }

   /**
    * @see RelationshipRole#isEnabled()
    */
   public boolean isEnabled() {
	   return _subject.isEnabled();
   }
   
   /**
    * @see RelationshipRole#setEnabled(boolean)
    */
   public void setEnabled(boolean flag) {
	   _subject.setEnabled( flag );
   }

   /**
    * @see RelationshipRole#isOriginMany()
    */
   public boolean isOriginMany() {
	   return _subject.isOriginMany();
   }

   /**
    * @see RelationshipRole#isTargetMany()
    */
   public boolean isTargetMany() {
	   return _subject.isTargetMany();
   }
   
   /**
    * @see RelationshipRole#setTargetMany(boolean)
    */
   public void setTargetMany(boolean flag) {
	   _subject.setTargetMany( flag );
   }

   /**
    * @see RelationshipRole#isOriginPrimaryKey()
    */
   public boolean isOriginPrimaryKey() {
	   return _subject.isOriginPrimaryKey();
   }

   /**
    * @see RelationshipRole#isTargetPrimaryKey();
    */
   public boolean isTargetPrimaryKey() {
	   return _subject.isTargetPrimaryKey();
   }

   /**
    * @see RelationshipRole#getColumnMaps()
    */
   public ColumnMap[] getColumnMaps() {
	   return _subject.getColumnMaps();
   }

   /**
    * @see RelationshipRole#getOrigin(Plugin)
    */
   public Table getOrigin(Plugin plugin) {
	   return (Table)_originTables.get( plugin );
   }
   
   /**
    * @see RelationshipRole#getTarget(Plugin)
    */
   public Table getTarget(Plugin plugin) {
	   return (Table)_targetTables.get( plugin );
   }

   /**
    * @see RelationshipRole#getRelation()
    */
   public Relation getRelation() {
	   return _subject.getRelation();
   }

   /**
    * @see RelationshipRole#getOrigin()
    */
   public Table getOrigin() {
	   return _origin;
   }

   /**
    * @see RelationshipRole#getTarget()
    */
   public Table getTarget() {
	   return _target;
   }
   
   /**
    * @see RelationshipRole#setTargetRole(RelationshipRole)
    */
   public void setTargetRole(RelationshipRole targetRole) {
	   _subject.setTargetRole( targetRole );
   }

   /**
    * @see RelationshipRole#isMandatory()
    */
   public boolean isMandatory() {
	   return _subject.isMandatory();
   }
   
   /**
    * Get the preferences prefix value.
    *
    * @return The prefix for the preferences file.
    */
   protected final String prefsPrefix() {
      return getPlugin().getName() + "/relations/" + getRelation().getName() + "/" + getName();
   }
}
